//
//  AppDelegate.m
//  JxCarios
//
//  Created by xcq on 15/1/19.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "AppDelegate.h"
#import "RootBaseController.h"
#import "ConfigDefine.h"
#import "CurrentDataSource.h"
#import <ShareSDK/ShareSDK.h>
#import <QZoneConnection/ISSQZoneApp.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <WeChatConnection/WeChatConnection.h>
#import "WXApi.h"
@interface AppDelegate () <RequestorDelegate>{
    CurrentDataSource *dataSource;
}

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定     generalDelegate参数
    BOOL ret = [_mapManager start:MAPKEY  generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
    [self shareSDK];  //分享
    [self autoLogin];
    RootBaseController *base = [RootBaseController getInstance];
    base.delegate = self;
    self.window.rootViewController = base;
    [self setUpMapManager];
    [Common setLoginStatus:NO];
    return YES;
}

- (void)autoLogin {
    if ([Common currenAutoLogin]) {
        if (!dataSource) {
            dataSource = [CurrentDataSource dataSourceWithRequestor:self];
        }
        dataSource.userName = [Common userName];
        dataSource.passWord = [Common currentPassword];
        [dataSource userLogin:^(id result) {
            [Common setLoginStatus:YES];
            [Common setUserInfo:[result customObjectForkey:@"memberinfo"]];
        } andFail:^(id result) {
            
        }];
    }
}

- (BOOL)application:(UIApplication *)application
      handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url
                        wxDelegate:self];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [ShareSDK handleOpenURL:url
                 sourceApplication:sourceApplication
                        annotation:annotation
                        wxDelegate:self];
}

//一键分享
-(void)shareSDK
{
    [ShareSDK registerApp:@"416f1e643515"];     //参数为ShareSDK官网中添加应用后得到的AppKey
    
    //连接短信分享
    [ShareSDK connectSMS];
    
    /**
     连接新浪微博开放平台应用以使用相关功能，此应用需要引用SinaWeiboConnection.framework
     http://open.weibo.com上注册新浪微博开放平台应用，并将相关信息填写到以下字段
     **/
    [ShareSDK connectSinaWeiboWithAppKey:@"568898243"
                               appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                             redirectUri:@"http://www.sharesdk.cn"];
    
    
    //    /**
    //     连接QQ空间应用以使用相关功能，此应用需要引用QZoneConnection.framework
    //     http://connect.qq.com/intro/login/上申请加入QQ登录，并将相关信息填写到以下字段
    //
    //     如果需要实现SSO，需要导入TencentOpenAPI.framework,并引入QQApiInterface.h和TencentOAuth.h，将QQApiInterface和TencentOAuth的类型传入接口
    //     **/
    [ShareSDK connectQZoneWithAppKey:@"100371282"
                           appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
                   qqApiInterfaceCls:[QQApiInterface class]
                     tencentOAuthCls:[TencentOAuth class]];
    //添加微信应用 注册网址 http://open.weixin.qq.com
    [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"
                           wechatCls:[WXApi class]];
    
    //添加QQ应用
    [ShareSDK connectQQWithQZoneAppKey:@"100371282"                 //该参数填入申请的QQ AppId
                     qqApiInterfaceCls:[QQApiInterface class]
                       tencentOAuthCls:[TencentOAuth class]];
    
    
    
}


- (void)setUpMapManager{
    
}

- (void)onGetNetworkState:(int)iError
{
    if (0 == iError) {
        NSLog(@"联网成功");
    }
    else{
        NSLog(@"onGetNetworkState %d",iError);
    }
    
}

- (void)onGetPermissionState:(int)iError
{
    if (0 == iError) {
        NSLog(@"授权成功");
    }
    else {
        NSLog(@"onGetPermissionState %d",iError);
    }
}
//FIXME
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    //TODO:判断点击的是哪个tabbar
}


- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
